package com.huike.pmps.controller;

import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/roles")
public class RoleController {
    @Autowired
    RoleService roleService;

    @ApiOperation(value = "添加角色", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "角色名称", paramType = "query", dataType = "String", required = true)})
    @PostMapping
    public Result insertOne(String name) {
        return roleService.insertOne(name);
    }


    @ApiOperation(value = "根据角色ID删除角色", httpMethod = "DELETE")
    @DeleteMapping("/{roleId}")
    public Result deleteByRoleId(@PathVariable("roleId") Integer roleId) {
        return roleService.deleteByRoleId(roleId);
    }


    @ApiOperation(value = "根据角色ID，修改角色名称", httpMethod = "PATCH")
    @PatchMapping("/name/{roleId}")
    public Result updateByRoleId(@PathVariable("roleId") Integer roleId, String name) {
        return roleService.updateByRoleId(roleId, name);
    }


    @ApiOperation(value = "角色启用或禁用", httpMethod = "PATCH")
    @ApiImplicitParam(name = "state", value = "角色状态", paramType = "query", dataType = "int", required = true)
    @PatchMapping("/state/{roleId}")
    public Result updateStateByRoleId(@PathVariable("roleId") Integer roleId, Integer state) {
        return roleService.updateStateByRoleId(roleId, state);
    }


    @ApiOperation(value = "按搜索条件分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "角色状态", paramType = "query", dataType = "int")
    })
    @GetMapping
    public Result selectByCondition(PageInfo pageInfo, String name, Short state) {
        return roleService.selectByCondition(pageInfo, name, state);
    }
    @ApiOperation(value = "获取单个角色详细信息", httpMethod = "GET")
    @GetMapping("/{roleId}")
    public Result selectOneById(@PathVariable("roleId") Integer roleId) {
        return roleService.selectOneById(roleId);
    }

    @ApiOperation(value = "给角色分配权限", httpMethod = "POST")
    @ApiImplicitParam(name = "permissionIds", value = "权限ID集合(权限ID按 逗号 拼接)", paramType = "query", dataType = "String")
    @PostMapping("/role_permission/{roleId}")
    public Result insertMemberAndRoleRelation(@PathVariable("roleId") Integer roleId, String permissionIds) {
        return roleService.insertRoleAndPermissionRelation(roleId, permissionIds);
    }

}
